﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hartley.Util
{
   public class SportsUtil
    {
        public string encryptQueryStrings(string queryString)
        {
            int number;
            string encryptedString = "";
            bool convertableToInteger = Int32.TryParse(queryString, out number);
            if (convertableToInteger)
            {
                //this is for year
                double encryptedInteger = (Math.Pow(number, 2) + 2 * number + 1);
                encryptedString = encryptedInteger.ToString();
            }
            else
            {
                //Replace the first,third, one before the last characres with addition of 5 places from their location in alphabetical order
                if (queryString.Length >= 3)
                {
                    //Get the first character
                    char firstCharacter = queryString[0];
                    char thirdCharacter = queryString[2];
                    char oneBeforeLastCharacter = queryString[queryString.Length - 2];

                    //Modify the first character in that string in the said positions

                    string encryptedString1 = getNextCharacter(firstCharacter).ToString()+ queryString;
                    string encryptedString2 = getNextCharacter(oneBeforeLastCharacter).ToString() + encryptedString1;
                    encryptedString = encryptedString2 + getNextCharacter(thirdCharacter).ToString();
                }
            }

            return encryptedString;
        }



        public string decryptQueryStrings(string queryString)
        {
            double number;
            string decryptedString = "";
            bool convertableToDouble = Double.TryParse(queryString, out number);
            if (convertableToDouble)
            {
                //this is for year
                double decryptedInteger = Math.Sqrt(number) - 1;
                decryptedString = decryptedInteger.ToString();
            }
            else
            {
                //Replace the first,third, one before the last characres with addition of 5 places from their location in alphabetical order
                if (queryString.Length >= 3)
                {
                    //Get the first character Now firts,2nd would be added character, hence remove them
                    char firstCharacter = queryString[0];
                    char thirdCharacter = queryString[2];
                    char oneBeforeLastCharacter = queryString[queryString.Length - 2];

                    //Modify the first character in that string in the said positions
                   
                    
                    //Remove first character
                    string decryptedString1 = queryString.Remove(0,1);
                    //Remove 2nd character
                    string decryptedString2 = decryptedString1.Remove(0,1);
                    //Remove 3rd character
                    decryptedString = decryptedString2.Remove(decryptedString2.Length -1,1);
                }
            }

            return decryptedString;
        }

        private char getNextCharacter(char c)
        {
            char letter = c;
            char nextChar;
            if (letter == 'x')
                nextChar = 'a';
            else if (letter == 'X')
                nextChar = 'A';
            else if (letter == 'y')
                nextChar = 'b';
            else if (letter == 'Y')
                nextChar = 'B';
            else if (letter == 'z')
                nextChar = 'c';
            else if (letter == 'Z')
                nextChar = 'C';
            else nextChar = (char)(((int)letter) + 3);

            return nextChar;
        }


        private char getPreviousCharacter(char c)
        {
            char letter = c;
            char prevChar;
            if (letter == 'a')
                prevChar = 'x';
            else if (letter == 'A')
                prevChar = 'X';
            else if (letter == 'b')
                prevChar = 'y';
            else if (letter == 'B')
                prevChar = 'Y';
            else if (letter == 'c')
                prevChar = 'z';
            else if (letter == 'C')
                prevChar = 'Z';
            else prevChar = (char)(((int)letter) - 3);

            return prevChar;
        }





    }
}
